package mo.qmul.gemom;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.GridBagConstraints;

/** COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 * */

public class ConnectDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jPanel_input = null;
	private JPanel jPanel_button = null;
	private JLabel jLabel_namespace = null;
	private JTextField jTextField_namespace = null;
	private JLabel jLabel_client = null;
	private JComboBox jComboBox_domains = null;
	private JButton jButton_add = null;
	private JButton jButton_cancel = null;
	
	private ConnectionControl up;
	
	private stateRecord sr;
	
	/**
	 * @param owner
	 */
	public ConnectDialog(ConnectionControl owner) {
		super(owner);
		initialize();
		up=owner;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(384, 127);
		this.add(getJPanel_input(), BorderLayout.CENTER);
		this.add(getJPanel_button(), BorderLayout.SOUTH);

		Dimension scr = Toolkit.getDefaultToolkit().getScreenSize(); 
		Dimension frm = this.getSize(); 
		setLocation( (scr.width - frm.width) / 2, 
				(scr.height - frm.height)/2); 
	}

	/**
	 * This method initializes jPanel_input	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel_input() {
		if (jPanel_input == null) {
			jLabel_client = new JLabel();
			jLabel_client.setText("Domain:");
			jLabel_namespace = new JLabel();
			jLabel_namespace.setText("Namespace:");
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(2);
			gridLayout.setColumns(2);
			jPanel_input = new JPanel();
			jPanel_input.setLayout(gridLayout);
			jPanel_input.add(jLabel_namespace, null);
			jPanel_input.add(getJTextField_namespace(), null);
			jPanel_input.add(jLabel_client, null);
			jPanel_input.add(getJComboBox_domains(), null);
		}
		return jPanel_input;
	}

	/**
	 * This method initializes jPanel_button	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel_button() {
		if (jPanel_button == null) {
			jPanel_button = new JPanel();
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(1);
			jPanel_button.setLayout(gridLayout);
			jPanel_button.add(getJButton_add(), new GridBagConstraints());
			jPanel_button.add(getJButton_cancel(), new GridBagConstraints());
		}
		return jPanel_button;
	}

	/**
	 * This method initializes jTextField_namespace	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField_namespace() {
		if (jTextField_namespace == null) {
			jTextField_namespace = new JTextField();
		}
		return jTextField_namespace;
	}

	/**
	 * This method initializes jComboBox_domains	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox_domains() {
		String[] domains={"Client","Gbroker"};

		if (jComboBox_domains == null) {
			jComboBox_domains = new JComboBox(domains);


		}
		return jComboBox_domains;
	}

	/**
	 * This method initializes jButton_add	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton_add() {
		if (jButton_add == null) {
			jButton_add = new JButton();
			jButton_add.setText("ADD");
			jButton_add.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					AddAction();

				}
			});
		}
		return jButton_add;
	}

	/**
	 * This method initializes jButton_cancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton_cancel() {
		if (jButton_cancel == null) {
			jButton_cancel = new JButton();
			jButton_cancel.setText("CANCEL");
			jButton_cancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					CancelAction();

				}
			});
		}
		return jButton_cancel;
	}

	/**
	 * 
	 * 
	 * @return namespace: String
	 * */

	private String getNamespace()
	{

		String namespace=null;


		namespace=jTextField_namespace.getText();

		return namespace;

	}

	private String getDomain(){
		
		String namespace=null;


		namespace=jComboBox_domains.getSelectedItem().toString();

		return namespace;


	}

	private void AddAction(){


		sr= new stateRecord(getNamespace(),getDomain());
		
		up.buildMeasure();
		
		this.dispose();
	}
	
	private void CancelAction(){
		
		sr=null;
		
		this.dispose();
	}
	
	
	public stateRecord getstateRecord(){
		
		return sr;
	}


}  //  @jve:decl-index=0:visual-constraint="10,10"  
